package script.mainpage
{
	import laya.components.Script;
	import laya.events.Event;
	import laya.utils.Browser;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.Userdata;
	import model.order.OrderGoodsVo;
	import model.order.OrderVo;
	
	import script.ViewManager;
	import script.mainpage.items.OrderGoodsCell;
	
	import ui.tabview.OrderDetailPanelUI;
	
	import utils.UtilTool;
	
	public class OrderDetailController extends Script
	{
		private var uiSkin:OrderDetailPanelUI;
		
		private var createDate:Date;
		public var param:OrderVo;
		
		
		public function OrderDetailController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as OrderDetailPanelUI;
			
			uiSkin.goodsList.itemRender = OrderGoodsCell;
			
			uiSkin.goodsList.vScrollBarSkin = "";
			uiSkin.goodsList.spaceY = 5;
			uiSkin.goodsList.renderHandler = new Handler(this, updateGoodsList);
			//uiSkin.userCartList.selectHandler = new Handler(this,selectGoods);
			//uiSkin.userCartList.selectEnable = true;
			uiSkin.goodsList.array = [];
			
			createDate = new Date(param.createTime);

			var leftime = (new Date()).getTime() - createDate.getTime();
			
			if(param.orderState == 0 && param.overTime == 0)
			{
				if(leftime/1000 > 3600)
				{
					this.uiSkin.title.text = "支付超时";
					this.uiSkin.leftTime.text = "00:00:00";
				}
				else
				{
					Laya.timer.loop(1000,this,countDown);
	
					this.uiSkin.leftTime.text = "请在 00:" + UtilTool.getCountDownString(3600 - Math.floor(leftime/1000)) + "内支付" ;
						
				}
			
			}
			else if(param.orderState == 1)
			{
				this.uiSkin.title.text = "待取货";
				uiSkin.leftTime.text = "";
				uiSkin.cancelOrder.visible = false;
				uiSkin.payNow.visible = false;
			}
			else if(param.orderState == 2)
			{
				this.uiSkin.title.text = "订单已取消";
				uiSkin.leftTime.text = "";
				uiSkin.cancelOrder.visible = false;
				uiSkin.payNow.visible = false;
			}
			
			
			uiSkin.userName.text = Userdata.instance.userName + "  " + Userdata.instance.userAccount;
			
			uiSkin.goodPrice.text = "¥" +param.orderAmount + "";
			uiSkin.finalPrice.text = "¥" +param.orderAmount + "";
			uiSkin.orderIdlbl.text = param.orderId;
			uiSkin.orderTime.text = param.createTime;
			
			uiSkin.payPrice.text = "¥" +param.orderAmount + "";
			
			uiSkin.cancelOrder.on(Event.CLICK,this,onCancelOrder);
			
			uiSkin.closeBtn.on(Event.CLICK,this,function()
			{
				
				ViewManager.instance.closeView(ViewManager.VIEW_ORDER_DETAIL_PANEL);
			});
			
			uiSkin.copytBnt.on(Event.CLICK,this,copyId);
			uiSkin.payNow.on(Event.CLICK,this,function(){
				
				ViewManager.instance.openView(ViewManager.VIEW_CONFIRM_PAY_PANEL,false,{money:param.orderAmount,orderId:param.orderId});

			});
			
			uiSkin.cancelOrder.on(Event.CLICK,this,cancelOrder);
			var postdata:String = "orderId=" + param.id;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getOrderDetail,this,onGetGoodsBack,postdata,"post");

				
				
		}
		
		private function updateGoodsList(cell:OrderGoodsCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		private function onGetGoodsBack(data:Object):void
		{
			if(uiSkin.destroyed)
				return;
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				var goodslist:Array = [];
				for(var i:int=0;i < result.data.length;i++)
				{
					goodslist.push(new OrderGoodsVo(result.data[i]));
				}
				
				uiSkin.goodsList.array = goodslist;
			}
			
		}
		
		private function countDown():void
		{
			var leftime = (new Date()).getTime() - createDate.getTime();
			
			if(leftime/1000 > 3600)
			{
				this.uiSkin.title.text = "支付超时";
				this.uiSkin.leftTime.text = "00:00:00";

				Laya.timer.clearAll(this);
			}
			else
			{
				this.uiSkin.leftTime.text = "请在 00:" + UtilTool.getCountDownString(3600 - Math.floor(leftime/1000)) + "内支付" ;
			}
		}
		
		private function onCancelOrder():void
		{
			
		}
		
		private function cancelOrder():void
		{
			ViewManager.instance.openView(ViewManager.VIEW_POPUPDIALOG,false,{caller:this,callback:confirmCancel,msg:"确定删除该订单吗？",cancel:"再想想"});
		}
		private function confirmCancel(b:Boolean):void
		{
			if(b)
			{
				var postdata:String = "orderId=" + param.id;
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.cancelOrder,this,onCancelBack,postdata,"post");
			}
		}
		
		private function onCancelBack(data:*):void
		{
			if(uiSkin.destroyed)
				return;
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				uiSkin.cancelOrder.visible = false;
				uiSkin.payNow.visible = false;
				
				uiSkin.title.text ="订单已取消";
				uiSkin.leftTime.text = "";
				Laya.timer.clearAll(this);
			}
		}
		private function copyId():void
		{
			var dateInput = Browser.document.createElement("input");				
			
			dateInput.type ="text";
			dateInput.value = uiSkin.orderIdlbl.text;
			Browser.document.body.appendChild(dateInput);//添加到舞台
			dateInput.select();
			
			window.document.execCommand('copy');
			Browser.document.body.removeChild(dateInput);//添加到舞台		
		}
		
		public override function onDestroy():void{
			
			Laya.timer.clearAll(this);

		}
	}
}